package com.fgw.controller.backend;

import com.fgw.common.Const;
import com.fgw.common.ResponseCode;
import com.fgw.common.ServerResponse;
import com.fgw.pojo.Teacher;
import com.fgw.service.ITeacherlibService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.xml.ws.Response;

/**
 * Created by lyj on 2017/9/15.
 */
@Controller
@RequestMapping("/admin/Teacherlib/")
public class TeacherloginAdminController {

    @Autowired
    private ITeacherlibService iTeacherlibService;

    /**
     * 后台师资登录
     * @param
     * @param
     * @param session
     * @return
     */
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<UsernamePasswordToken> login(/*String tuser, String tpassword,*/ HttpSession session, Teacher teacher){

        /*Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
        try{
            subject.login(token);
            request.getSession().setAttribute("user", user);
            return "success";
        }catch(Exception e){
            e.printStackTrace();
            request.getSession().setAttribute("user", user);
            request.setAttribute("error", "用户名或密码错误！");
            return "login";
        }*/

        Subject subject= SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(teacher.getTuser(),teacher.getTpassword());

        try{
            subject.login(token);
            session.setAttribute(Const.CURRENT_USER,teacher);
            return ServerResponse.createBySuccess(token);
        }catch(Exception e){
            //e.printStackTrace();
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"用户名或密码错误！");
        }

        //ServerResponse<Teacher> response=iTeacherlibService.login(tuser,tpassword);
      /*  if(response!= null){
            Teacher teacher=response.getData();
            session.setAttribute(Const.CURRENT_USER,teacher);
            //return response;
        }

        return response;*/
    }











}
